# 查询

# 会话窗口
from sqlalchemy.orm import sessionmaker
from create_table import engine
Session = sessionmaker(engine)
db_session = Session()


"""
1. 查询
select * from user
"""
from create_table import User

# 1. 简单查询
# user_list = db_session.query(User).all()
#
# for row in user_list:
#     print(row.id,row.name)

# 2. 查询第一条数据
# user_obj = db_session.query(User).first()
# print(row.id,row.name)

# 3. 待条件的查询
# user_list = db_session.query(User).filter(User.id==3).all()
# print(user_list[0].name)

# 3. 待条件的查询 filter(User.id==3)
# user_list = db_session.query(User).filter(User.id==3).all()
# print(user_list[0].name)

# 4. 不适用表达式 filter_by(id=3)
# user_list = db_session.query(User).filter_by(id=3).all()
# print(user_list[0].name)
#
# user_list = db_session.query(User).filter_by(id=3).first()
# print(user_list.name)

# 5.条件 >= <=
# user_list = db_session.query(User).filter(User.id >= 3).all()
# print(user_list[0].name)

# 6.查看当前执行的sql语句  去掉 .all()
"""
SELECT user.id AS user_id, user.name AS user_name 
FROM user 
WHERE user.id >= %(id_1)s
"""
# sql = db_session.query(User).filter(User.id >= 3)
# print(sql)

user_list = db_session.query(User.name).all()
print(user_list)
for row in user_list:
    print(row.name)